package BNS.wheat;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.regex.*;
import BNS.graphs.*;
import BNS.plumbing.*;

/** This class builds a WheatMktGame by supplying all the parameters to construct it,
	and then stores it in a certain directory with suggested name.
	*/
public class GameBuilder extends JFrame implements ActionListener {
	private JTextField colorField, timeField, leanField, phatField, 
						preField, gameField, sufField, packField;
	private JSlider colorSlide;
	private JLabel gpLabel;
	private JButton submit;
	private String[] gpNames;
	private int gpNum=0;
	private JComboBox typeList;
	private String prefix;
	
	public GameBuilder(String[] args) {
		gpNames= args;
		System.err.println("making "+ args.length +" games");

		String prep= System.getProperty("PREP");
		if (prep==null) prep= "./";
		Library.setPrep(new File(prep));
		/* We discover the value of the ARCH property, which will be used
		in the Library for storing session files and exp files. */
		String arch= System.getProperty("ARCH");
		if (arch==null) arch= "./output";
		Library.setArch(new File(arch));

		setTitle("BNS Game GameBuilder");
		setSize(500,190);
		JPanel panel= new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
//		JPanel typePanel= new JPanel();
//		typePanel.setLayout(new BoxLayout(typePanel, BoxLayout.X_AXIS));
//		typePanel.add(new JLabel("game type:",JLabel.RIGHT));		
//		String[] typeStrings= { "WheatMarket" };
//		typeList= new JComboBox(typeStrings);
//		typeList.addActionListener(this);
//		typePanel.add(typeList);
//		panel.add(typePanel);		
		
		JPanel packPanel= new JPanel();
		packPanel.setLayout(new BoxLayout(packPanel, BoxLayout.X_AXIS));
		packPanel.add(new JLabel("pack file:",JLabel.RIGHT));		
		packField= new JTextField(); packPanel.add(packField);
		panel.add(packPanel);
/*		
		JPanel colorPanel= new JPanel();
		colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.X_AXIS));
		colorPanel.add(new JLabel("number of colors:",JLabel.RIGHT));
		colorField= new JTextField(3); colorPanel.add(colorField);
		panel.add(colorPanel);
		
		JPanel payPanel= new JPanel();
		payPanel.setLayout(new BoxLayout(payPanel, BoxLayout.X_AXIS));
		payPanel.add(new JLabel("payoffs:",JLabel.RIGHT));		
		leanField= new JTextField("1.00",5); payPanel.add(leanField);
		phatField= new JTextField("2.00",5); payPanel.add(phatField);
		panel.add(payPanel);
*/		
		JPanel timePanel= new JPanel();
		timePanel.setLayout(new BoxLayout(timePanel, BoxLayout.X_AXIS));
		timePanel.add(new JLabel("game time limit:",JLabel.RIGHT));		
		timeField= new JTextField("120",5); timePanel.add(timeField);
		panel.add(timePanel);
		
		JPanel gamePanel= new JPanel();
		gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.X_AXIS));
		gamePanel.add(new JLabel("game file:",JLabel.RIGHT));
		preField= new JTextField(10); gamePanel.add( preField);	preField.setText("Mkt-");
		gameField=new JTextField(50); gamePanel.add(gameField);
		sufField= new JTextField(".game",10); gamePanel.add( sufField);
		panel.add(gamePanel);
		
		submit= new JButton("submit"); submit.addActionListener(this); panel.add(submit);
		getContentPane().add(panel);
		validate();	// ?
//		pack();
		setLocation(700,60);
		setVisible(true);
		nextPack();
		}
	
	private void nextPack() {
		if (gpNum < gpNames.length) {
			packField.setText(gpNames[gpNum]);
			String pack= new File(gpNames[gpNum]).getName();
//			System.err.println("gpack suffix found at "+ pack.indexOf(".gpack"));
			int sufidx= pack.indexOf(".gpack");
			if (sufidx<1) sufidx= pack.length();
			String fname= pack.substring(0,sufidx);
			Pattern pattern = Pattern.compile("c=\\d*");
			Matcher matcher = pattern.matcher(fname);
			if (matcher.find()) {
				int st= matcher.start();
				int nd= matcher.end();
				String cpull= fname.substring(st+2,nd);
				int chroma= Integer.parseInt(cpull);
				//System.err.format("matched (%d,%d) found %s%n",st,nd,cpull);
				System.err.println("chromatic number found "+ chroma);
				if (chroma>2) colorField.setText(""+chroma);
				}
			gameField.setText(fname);
			}
		else if (gpNum == gpNames.length) {
			packField.setText("");
			gameField.setText("");
			}
		gpNum++;
		}
	
	private void makeAndSave() {
		File pfile= new File(packField.getText());
		System.err.println("obtaining gpack at "+ pfile);
		GraphPack gp= Library.getPack(pfile);
		if (gp==null) return;
//		String type= (String)typeList.getSelectedItem();
		int time= Integer.parseInt(timeField.getText());
		String prefix= preField.getText();
		String suffix= sufField.getText();
		String gfld= prefix + gameField.getText() + suffix;
		System.err.println(" params: "+ time);
		BNSGameController game= null;
		game= new WheatMktGame(gp,time);
		if (game!=null) {
			System.err.println("game obtained: "+ game.getInfo());
			Library.putGame(new File(gfld),game);
			System.err.println("saved game at "+ gfld);
			}
		else System.err.println("FAILED to construct game ");
		}
	
	public void actionPerformed(ActionEvent e) { 
		Object src= e.getSource();
		if (src==submit) { makeAndSave(); nextPack(); }
//		if (src==typeList) { 
//			String type= (String)typeList.getSelectedItem();
//			preField.setText("");
//			if (type.equals("WheatMarket"))  preField.setText("Mkt-");
//			}
		}
	
	public static void main(String[] args) { new GameBuilder(args); }
	
	} // EOClass GameBuilder
